/*
 *
 * Copyright(c) TODO
 * licensing@realtor.org
 * http://www.realtor.org/license
 */

Ext.onReady(function()
{
	AD.Data.num_store = [
		 [ '', 'Any' ]
		,[ 1, '1' ]
		,[ 2, '2' ]
		,[ 3, '3' ]
		,[ 4, '4' ]
		,[ 5, '5' ]
		,[ '6@@', '6 or more' ]
	];

	AD.Data.mls_store = [
		 'MN_Minnesota'
		,'OH_Cincinnati'
		,'CA_SanDiego'
	];

	AD.Data.date_store = [
		 [ '', 'Any' ]
		,[ 7, 'Within a week' ]
		,[ 30, 'Within a month' ]
		,[ 90, 'Within 3 months' ]
		,[ 182, 'Within 6 months' ]
		,[ 365, 'Within 1 year' ]
		,[ 730, 'Within 2 years' ]
		,[ 1095, 'Within 3 years' ]
		,[ 1460, 'Within 4 years' ]
		,[ 1825, 'Within 5 years' ]
		,[ 2190, 'Within 6 years' ]
		,[ 2555, 'Within 7 years' ]
		,[ 2920, 'Within 8 years' ]
		,[ 3285, 'Within 9 years' ]
		,[ 3650, 'Within 10 years' ]
	];

	AD.Data.dollar_store = [
		 [ '', 'Any' ]
        ,[ '0-100000', 'Less Than $100,000' ]
		,[ '100000-200000', '$100,000-$200,000' ]
		,[ '200000-300000', '$200,000-$300,000' ]
		,[ '300000-400000', '$300,000-$400,000' ]
		,[ '400000-500000', '$400,000-$500,000' ]
		,[ '500000-600000', '$500,000-$600,000' ]
		,[ '600000-700000', '$600,000-$700,000' ]
		,[ '700000-800000', '$700,000-$800,000' ]
		,[ '800000-900000', '$800,000-$900,000' ]
		,[ '900000-1000000', '$900,000-$1,000,000' ]
		,[ '1000000@@', '$1,000,000 or more' ]
		,[ '2000000@@', '$2,000,000 or more' ]
	];

	AD.Data.sqft_store = [
		 [ '', 'Any' ]
        ,[ '0-500', 'Less Than 500' ]
		,[ '500-1000',  '500-1000' ]
		,[ '1000-2000', '1000-2000' ]
		,[ '2000-3000', '2000-3000' ]
		,[ '3000-4000', '3000-4000' ]
		,[ '4000-5000', '4000-5000' ]
		,[ '5000-6000', '5000-6000' ]
		,[ '6000-7000', '6000-7000' ]
		,[ '7000-8000', '7000-8000' ]
		,[ '8000-9000', '8000-9000' ]
		,[ '9000-10000', '9000-10,000' ]
		,[ '10000@@', '10,000 or more' ]
	];

	var today = new Date();
	var year = today.getFullYear();

	AD.Data.year_store = [[ '', 'Any' ]];

	for (var i = 0; i < 100; i += 10)
	{
		AD.Data.year_store.push( [ (year -i -10)  + '-' + (year -i), (year -i -10)  + '-' + (year -i) ] );
	}

	AD.Data.make_ajax_store = function(my_url)
	{
		return new Ext.data.Store( {url : my_url, reader : new Ext.data.XmlReader( {record : "row",	id : "key"}, [ 'key', 'value' ])});
	};

	AD.Data.make_lookup_store = function(lookup)
	{
		var my_url = AD.Globals.URL_server + 'lookup&field=' + lookup;
		return AD.Data.make_ajax_store(my_url);
	};

	AD.Data.rec_search = Ext.data.Record.create(
	[
		 {name : 'acres'}
		,{name : 'annual_assessment'}
		,{name : 'apn'}
		,{name : 'appliances'}
		,{name : 'area'}
		,{name : 'basement'}
		,{name : 'basement_feat'}
		,{name : 'bath_rooms',					type : 'float'}
		,{name : 'bed_rooms',					type : 'float'}
		,{name : 'bedroom_2_area'}
		,{name : 'bedroom_3_area'}
		,{name : 'bedroom_4_area'}
		,{name : 'bedroom_5_area'}
		,{name : 'city'}
		,{name : 'comments'}
		,{name : 'cooling'}
		,{name : 'county'}
		,{name : 'dining_room_area'}
		,{name : 'directions'}
		,{name : 'exterior'}
		,{name : 'fireplaces'}
		,{name : 'full_bathrooms'}
		,{name : 'garage'}
		,{name : 'garage_description'}
		,{name : 'half_bathrooms'}
		,{name : 'heating'}
		,{name : 'kitchen_area'}
		,{name : 'last_modified_date_time'}
		,{name : 'latitude'}
		,{name : 'listing_agent_first_name'}
		,{name : 'listing_agent_last_name'}
		,{name : 'listing_date'}
		,{name : 'listing_office_name'}
		,{name : 'listing_price',				type : 'float'}
		,{name : 'longitude'}
		,{name : 'lot_dimension'}
		,{name : 'master_bedroom_area'}
		,{name : 'ml_number'}
		,{name : 'other_buildings'}
		,{name : 'picture_count'}
		,{name : 'picture_modified_date_time'}
		,{name : 'property_type'}
		,{name : 'school_district'}
		,{name : 'selling_agent_first_name'}
		,{name : 'selling_agent_last_name'}
		,{name : 'selling_date'}
		,{name : 'selling_price',				type : 'float'}
		,{name : 'sewer'}
		,{name : 'sqft_above_ground'}
		,{name : 'sqft_below_ground'}
		,{name : 'state'}
		,{name : 'status'}
		,{name : 'status_date'}
		,{name : 'street_direction'}
		,{name : 'street_name'}
		,{name : 'street_number', type : 'float'}
		,{name : 'street_post_direction'}
		,{name : 'street_suffix'}
		,{name : 'style'}
		,{name : 'sub_division'}
		,{name : 'three_quarter_bathrooms'}
		,{name : 'unit_number', type : 'float'}
		,{name : 'view'}
		,{name : 'water'}
		,{name : 'year_built', type : 'float'}
		,{name : 'zip'}
		,{name : 'zoning'}
	]);

	AD.Data.rdr_search = new Ext.data.XmlReader( {record : "row",id : "ml_number"}, AD.Data.rec_search);
	AD.Data.proxy_search = new Ext.data.HttpProxy( {url : AD.Globals.URL_AD});
	AD.Data.ds_search = new Ext.data.Store( {proxy : AD.Data.proxy_search,reader : AD.Data.rdr_search});

	AD.UI.ds_search_clear = function()
	{
		AD.Data.ds_search.removeAll();
	};

	AD.Data.ds_search.on('load', function()
	{
		AD.UI.map_goog_clear();
		AD.UI.map_bing_clear();

		load_mask.hide();
		var tot = Ext.DomQuery.selectValue("total",	AD.Data.rdr_search.xmlData.documentElement);
		if (tot == undefined)
		{
			tot = 0;
		}
		var t = "Showing " + AD.Data.ds_search.totalLength + " of " + tot + " results.";
		AD.UI.results_grid.setTitle(t);

		if(0 < AD.Data.ds_search.totalLength)
		{
			AD.UI.enable_reports(true);
		}
		else
		{
			AD.UI.enable_reports(false);
			return;
		}

		if(AD.Data.ds_search.totalLength < AD.Globals.MAX_PROPS)
		{
			enable_field("SELECT_ALL", true);
		}
		else
		{
			enable_field("SELECT_ALL", false);
		}
		if("" != AD.UI.comps_to_load)
		{
			var ar_fv = AD.UI.comps_to_load.split(",");
			var grid = Ext.getCmp("results_grid");
			var sm =  grid.getSelectionModel();
			var store =grid.getStore(); 

			for(i=0; i< ar_fv.length; i++)
			{
				row = store.find('ml_number', ar_fv[i]);
				sm.selectRow(row, true);
			}
		}
	});
	
	AD.UI.fn_get_map_overlay_text = function(data, address, status)
	{
		var tx_data;
		tx_data  = ' Status = <b>' + data.status + '</b>';
		tx_data += '<br/> List price = <b>' + data.listing_price+  '</b>.<br/>List Date =  <b>' + data.listing_date + '</b>';
		
		if(status ==  'S')
		{
			tx_data += '<br /> Sale price = <b>' + data.selling_price +  '</b>.<br/>Sale Date = <b>' + data.selling_date + '</b>';
		}
		    
		var img = "<br /><img src=index.php?cmd=server&sub_cmd=img&img_index=0&ml_number=" + data.ml_number + " alt='property_image' width='100px'/>";
		
		return ('Mls: <b>' + data.ml_number + '</b><br/>' + address  + '<br />' + tx_data + img);
	}
	
	AD.UI.fn_get_status = function (data)
	{
		var status = "U";
		try{
			status = data.status.substring(0, 1).toUpperCase();
		}catch(rr){	}

		switch(status)
		{
			case "A":
			case "P":
			case "S":
				break;
			default:
				status = "U";
			break;
		}
		return status;
	}	
});